package com.riant.exception;

import com.riant.bean.ParamsException;
import com.riant.bean.response.BaseRes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Classname GlobalExceptionHandler
 * @Description TODO
 * @Date 2022/5/2 0:17
 * @Created by 张斌
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(ParamsException.class)
    @ResponseBody
    public  BaseRes<Object> doParamsException(ParamsException exception){
        final BaseRes<Object> objectBaseRes = new BaseRes<>();
        objectBaseRes.setCode(exception.getCode());
        objectBaseRes.setMsg(exception.getMessage());
        return objectBaseRes;
    }

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)

    @ResponseBody
    public  BaseRes<Object> doMediaTypeException(){
        logger.info("请求方式不对");
        final BaseRes<Object> objectBaseRes = new BaseRes<>();
        objectBaseRes.setCode("60000");
        objectBaseRes.setMsg("请求方式不正确");

        return objectBaseRes;
    }
}
